import time
from selenium.webdriver.common.by import By
from common.Util import BlogDriver

class BlogLogin:
    url = ""
    driver = ""
    def __init__(self):
        self.url = "http://8.137.19.140:9090/blog_login.html"
        self.driver = BlogDriver.driver
        self.driver.get(self.url)

    # 登录成功测试
    def LoginSucTest(self):
        time.sleep(2)
        # 清空输入框
        self.driver.find_element(By.CSS_SELECTOR,"#username").clear()
        self.driver.find_element(By.CSS_SELECTOR,"#password").clear()
        # 输入正确账号和密码
        self.driver.find_element(By.CSS_SELECTOR,"#username").send_keys("zhangsan")
        self.driver.find_element(By.CSS_SELECTOR,"#password").send_keys("123456")
        # 点击登录
        self.driver.find_element(By.CSS_SELECTOR,"#submit").click()
        # 对登录结果进⾏检测, 如果跳转到了博客列表⻚才算是登录成功了
        self.driver.find_element(By.CSS_SELECTOR,"body > div.container > div.left > div > img")
        # 截图
        BlogDriver.GetScreenShot()
        print("登录成功")
        # 返回登录页面
        # self.driver.back()


    # 登录失败测试
    def LoginFailTest(self,name,password,expect):
        self.driver.find_element(By.CSS_SELECTOR,"#username").clear()
        self.driver.find_element(By.CSS_SELECTOR,"#password").clear()
        # 输入用户名和密码,通过外面传参形式
        self.driver.find_element(By.CSS_SELECTOR,"#username").send_keys(name)
        self.driver.find_element(By.CSS_SELECTOR,"#password").send_keys(password)
        self.driver.find_element(By.CSS_SELECTOR,"#submit").click()

        BlogDriver.GetScreenShot()
        alert = self.driver.switch_to.alert
        print(alert.text)
        assert expect == alert.text
        alert.accept()  # 点击同意按钮
        # self.driver.back()
